import { ConfigProvider, message } from 'ant-design-vue';
import cloneDeep from 'lodash/cloneDeep';
import skyBlue from '@/assets/styles/themeVar/skyBlue';
import peacockGreen from '@/assets/styles/themeVar/peacockGreen';

// 获取UUID
export const GetUUID = () => {
  // const model = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  const model = 'xxxxxxxxxxxxxxxx';
  return model.replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
  });
};

// 获取BIZ UUID
export const GetBIZUUID = () => {
  const model = 'biz-xxxx-xxxx-xxxx-xxxx';
  return model.replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
  });
};

// 格式化数值千分位
export const FormatNumToThous = (value: number | string) => {
  if (!value) return value;
  return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};

// 获取父节点
export const GetPopupContainer = (trigger: any) => trigger.parentNode;

// 判断移动端
export const IsMobile = !!navigator.userAgent.match(
  /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/,
);

// 设置主题样式
export const SetTheme = (type: string) => {
  let themeType;
  switch (type) {
    case 'peacockGreen':
      themeType = peacockGreen;
      break;
    case 'skyBlue':
      themeType = skyBlue;
      break;
    default:
      themeType = peacockGreen;
      break;
  }
  const body = document.getElementsByTagName('body')[0];
  body.classList.forEach((cla) => {
    body.classList.remove(cla);
  });
  body.classList.add(type);
  ConfigProvider.config({
    theme: {
      ...themeType,
    },
  });
};

// 设置组件风格
export const SetCompopnentStyle = (curStyle: any) => {
  const componentStyle = curStyle.value?.componentStyle;
  switch (componentStyle) {
    case 'innerLight':
      Object.assign(curStyle.value, {
        'box-shadow': '0 0 3rem rgba(100, 200, 255, 0.5) inset',
      });
      break;
    case 'halo':
      Object.assign(curStyle.value, {
        background:
          'radial-gradient(rgba(39, 73, 101, 0.5), rgba(39, 73, 101, 0.3), rgba(39, 73, 101, 0), rgba(39, 73, 101, 1))',
        'box-shadow': '0 0 2rem rgba(39, 73, 101, 0.1) inset',
      });
      break;
    case 'gradient':
      Object.assign(curStyle.value, {
        'border-image': 'linear-gradient(45deg,red,blue) 10%',
      });
      break;
    case 'fluxayRainbow':
      Object.assign(curStyle.value, {
        background:
          '-webkit-linear-gradient(30deg, #32c5ff 25%, #b620e0 50%, #f7b500 75%, #20e050 100%)',
        animation: 'fluxayRainbow 300s infinite linear',
      });
      break;
    case 'technology':
      Object.assign(curStyle.value, {
        'border-image-slice': '100 130 110 300 fill',
        'border-image-width': '10 13 11 30',
        'border-image-repeat': 'round',
      });
      break;
    case 'technology2':
      Object.assign(curStyle.value, {
        'border-image-slice': '6 120 6 120 fill',
        'border-image-width': '2 40 2 40',
        'border-image-repeat': 'stretch',
      });
      break;
    case 'technology3':
      Object.assign(curStyle.value, {
        'border-image-slice': '6 10 15 10 fill',
        'border-image-width': '0 40px 17px 40px',
        'border-image-repeat': 'stretch',
      });
      break;
    case 'technology4':
      Object.assign(curStyle.value, {
        'border-image-slice': '47 200 60 200 fill',
        'border-image-width': '11px 40px 43px 40px',
        'border-image-repeat': 'stretch',
      });
      break;
    case 'lightSensation':
    case 'blink':
      break;
    default:
      break;
  }
};

// 统一处理后端返回结果
export const ResponseHandle = (res: any, callback: Function = () => null) => {
  if (res.code === 0) {
    callback && callback();
  } else {
    message.warning(
      typeof res.message === 'object'
        ? JSON.stringify(res.message)
        : res.message || '操作失败，请稍后再试',
    );
  }
};
